/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.dishes.impl;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.qinyeit.serviceapp.entity.DishesMenu;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyeit.serviceapp.service.dishes.DishesMenuService;
import com.qinyetech.springstage.core.lang.QyMap;
import com.qinyetech.springstage.core.lang.StringUtils;
import com.qinyetech.springstage.core.repository.jdbc.PageNamedParameterJdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qinyetech.springstage.core.service.BaseService;
import com.qinyeit.serviceapp.entity.DishesMenuDetail;
import com.qinyeit.serviceapp.repository.dishes.DishesMenuDetailRepository;
import com.qinyeit.serviceapp.service.dishes.DishesMenuDetailService;

import java.util.List;
import java.util.Map;

/** 
 * ClassName: DishesMenuDetailServiceImpl <br/> 
 * Function: 菜谱明细 业务实现 <br/> 
 * date: 2018-11-26 14:37:02 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class DishesMenuDetailServiceImpl extends BaseService<DishesMenuDetail, Long> implements DishesMenuDetailService {


	@Autowired
	private DishesMenuDetailRepository dishesMenuDetailRepository;
	@Autowired
	private PageNamedParameterJdbcTemplate jdbcTemplate;
	@Autowired
	private DishesMenuService dishesMenuService;


	@Override
	public List<Map<String, Object>> getMenuDetails(Long meunId) {
		if(meunId==null){
			throw new BusinessRunTimeException("请选择菜谱",10001);
		}
		DishesMenu dishesMenu = dishesMenuService.findById(meunId);
		if (StringUtils.isBlank(dishesMenu.getDishesDetails())){
			return Lists.newArrayList();
		}
		List<Long> dishesIds = JSONArray.parseArray(dishesMenu.getDishesDetails(),Long.class);

		String sql = "SELECT s.spec_id, s.spec_name, s.price, s.vip_price, s.def\n" +
				"\t, s.dishes_id, d.name AS dishes_name, d.no AS dishes_no, d.pinyin AS dishes_pinyin, d.spell AS dishes_spell\n" +
				"FROM dishes_specification_relations s\n" +
				"\tLEFT JOIN dishess d ON d.id = s.dishes_id\n" +
				"WHERE s.dishes_id " +
				"IN ( SELECT id from dishess where merchant_group_id = :merchant_group_id and type = 'DISHES' and id in(:dishesIds))";
		QyMap map = QyMap.map("menuId",meunId)
				.setv("merchant_group_id",dishesMenu.getMerchantGroupId())
				.setv("dishesIds",dishesIds);
		List<Map<String,Object>> mapList = jdbcTemplate.queryForList(sql,map);
		return mapList;
	}
}